﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace FinalProject
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                InitPage();
            }
            else
            {                                
                btnCapNhatTaiKhoan.Click += new EventHandler(btnCapNhatTaiKhoan_Click);
                ckbIsChangeEmail.CheckedChanged += new EventHandler(ckbIsChangeEmail_CheckedChanged);
                ckbIsChangePassword.CheckedChanged += new EventHandler(ckbIsChangePassword_CheckedChanged);
            }
        }

        void ckbIsChangePassword_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbIsChangePassword.Checked)
            {
                tbxMatKhauHienTai.Text = Utility.UserCurrent.StrPassword;
                tbxMatKhauMoi.Text = Utility.UserCurrent.StrPassword;
                tbxMatKhauXacNhanMoi.Text = Utility.UserCurrent.StrPassword;
                tbxMatKhauHienTai.Enabled = false;
                tbxMatKhauMoi.Enabled = false;
                tbxMatKhauXacNhanMoi.Enabled = false;
            }
            else
            {
                tbxMatKhauHienTai.Text = "";
                tbxMatKhauMoi.Text = "";
                tbxMatKhauXacNhanMoi.Text = "";
                tbxMatKhauHienTai.Enabled = true;
                tbxMatKhauMoi.Enabled = true;
                tbxMatKhauXacNhanMoi.Enabled = true;
            }
            lblAlertMatKhauHienTai.Visible = false;
            lblAlertMatKhauMoi.Visible = false;
            lblAlertMatKhauXacNhanMoi.Visible = false;
        }

        void ckbIsChangeEmail_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbIsChangeEmail.Checked)
            {
                tbxEmailDangNhapMoi.Text = Utility.UserCurrent.StrEmail;
                tbxEmailXacNhanMoi.Text = Utility.UserCurrent.StrEmail;
                tbxEmailDangNhapMoi.Enabled = false;
                tbxEmailXacNhanMoi.Enabled = false;
            }
            else
            {
                tbxEmailDangNhapMoi.Text = "";
                tbxEmailXacNhanMoi.Text = "";
                tbxEmailDangNhapMoi.Enabled = true;
                tbxEmailXacNhanMoi.Enabled = true;
            }
            lblAlertEmailDangNhapMoi.Visible = false;
            lblAlertEmailXacNhanMoi.Visible = false;
        }

        private void InitPage()
        {
            try
            {
                rdbNuocKhac.AutoPostBack = true;
                rdbVietNam.AutoPostBack = true;
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
                ckbIsChangeEmail.AutoPostBack = true;
                ckbIsChangePassword.AutoPostBack = true;
                lblEmailHienTai.Text = Utility.UserCurrent.StrEmail;
                tbxHo.Text = Utility.UserCurrent.StrLastname;
                tbxTen.Text = Utility.UserCurrent.StrFirstname;
                Date1.Text = Utility.UserCurrent.DtimeBirthday.ToString("M/d/yyyy");
            }
            catch { }
        }

        void btnCapNhatTaiKhoan_Click(object sender, EventArgs e)
        {
            try
            {                
                if (CheckValidation())
                {
                    if (ckbIsChangePassword.Checked)
                        Utility.UserCurrent.UpdateUserAccount(tbxEmailDangNhapMoi.Text, Utility.UserCurrent.StrPassword);
                    else
                        Utility.UserCurrent.UpdateUserAccount(tbxEmailDangNhapMoi.Text, tbxMatKhauMoi.Text);

                    int sex = rdbNam.Checked == true ? 1 : 0;
                    bool isVietnamese = rdbVietNam.Checked == true ? true : false;

                    string noiCuNguID;
                    if (isVietnamese)
                        noiCuNguID = Utility.LstTinhThanh[ddlTinhThanh.SelectedIndex - 1].ID;
                    else
                        noiCuNguID = Utility.LstQuocGia[ddlTinhThanh.SelectedIndex - 1].ID;

                    Utility.UserCurrent.UpdateUserInfo(tbxHo.Text, tbxTen.Text, DateTime.Parse(Date1.Text), sex, noiCuNguID, isVietnamese);
                    Session["username"] = Utility.UserCurrent.StrEmail;
                    Session["password"] = Utility.UserCurrent.StrPassword;
                    Response.Redirect("CapNhatTaiKhoanThanhCong.aspx");
                }
                else
                {
                }
            }
            catch { }
        }        

        #region Check Validation
        private bool CheckEmail(string email)
        {
            if (email.Length < 4)
            {
                return false;
            }
            else
            {
                if (email.IndexOf(' ') > 0)
                    return false;

                if (email.IndexOf('@') <= 0 || email.IndexOf('@') != email.LastIndexOf('@'))
                    return false;

                if (email.Contains("..") || email.Contains("@.") || email.Contains(".@"))
                    return false;

                if (email[0] == '.' || email[email.Length - 1] == '.')
                    return false;

                if (email.IndexOf('@') > email.LastIndexOf('.'))
                    return false;

                string str = "qwertyuiopasdfghjklzxcvbnm-_.@";
                for (int i = 0; i < email.Length; i++)
                {
                    if (!str.Contains(email[i]))
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        private bool CheckPasswordCu(string pass)
        {
            try
            {
                if (pass == Utility.UserCurrent.StrPassword)
                    return true;

                return false;
            }
            catch
            {
                return false;
            }
        }

        private bool CheckPassword(string pass)
        {
            if (pass.Length < 4)
                return false;

            if (pass.Contains(' '))
                return false;

            return true;
        }

        private bool CheckHoTen(string hoTen)
        {
            if (hoTen == "")
                return false;

            if (hoTen[0] == ' ' || hoTen[hoTen.Length - 1] == ' ')
                return false;

            if (hoTen.Contains('"') || hoTen.Contains('|') || hoTen.Contains('\\') || hoTen.Contains('\n') || hoTen.Contains('\r'))
                return false;

            string str = "`~1!2@3#4$5%6^7&8*9(0)-_=+/?.>,<';:[{]}";
            for (int i = 0; i < str.Length; i++)
            {
                if (hoTen.Contains(str[i]))
                    return false;
            }

            return true;
        }

        private bool CheckValidation()
        {
            bool isValidately = true;

            #region Check Email
            if (!ckbIsChangeEmail.Checked)
            {
                if (CheckEmail(tbxEmailDangNhapMoi.Text))
                {
                    lblAlertEmailDangNhapMoi.Visible = false;
                    if (tbxEmailDangNhapMoi.Text != tbxEmailXacNhanMoi.Text)
                    {
                        lblAlertEmailXacNhanMoi.Visible = true;
                        isValidately = false;
                    }
                    else
                    {
                        lblAlertEmailXacNhanMoi.Visible = false;
                    }
                }
                else
                {
                    lblAlertEmailDangNhapMoi.Visible = true;
                    lblAlertEmailXacNhanMoi.Visible = true;
                    isValidately = false;
                }
            }
            #endregion            

            if (!ckbIsChangePassword.Checked)
            {
                #region Check Password Cu
                if (!CheckPasswordCu(tbxMatKhauHienTai.Text))
                {
                    lblAlertMatKhauHienTai.Visible = true;
                    isValidately = false;
                }
                else
                {
                    lblAlertMatKhauHienTai.Visible = false;
                }
                #endregion

                #region Check Password Moi
                if (CheckPassword(tbxMatKhauMoi.Text))
                {
                    lblAlertMatKhauMoi.Visible = false;
                    if (tbxMatKhauMoi.Text != tbxMatKhauXacNhanMoi.Text)
                    {
                        lblAlertMatKhauXacNhanMoi.Visible = true;
                        isValidately = false;
                    }
                    else
                    {
                        lblAlertMatKhauXacNhanMoi.Visible = false;
                    }
                }
                else
                {
                    lblAlertMatKhauMoi.Visible = true;
                    lblAlertMatKhauXacNhanMoi.Visible = true;
                    isValidately = false;
                }
                #endregion
            }

            #region Check Ho
            if (CheckHoTen(tbxHo.Text))
            {
                lblAlertHo.Visible = false;
            }
            else
            {
                lblAlertHo.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Ten
            if (CheckHoTen(tbxTen.Text))
            {
                lbtAlertTen.Visible = false;
            }
            else
            {
                lbtAlertTen.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Ngay thang nam sinh
            try
            {
                DateTime date = Convert.ToDateTime(Date1.Text);
                lblAlertNgaySinh.Visible = false;
            }
            catch
            {
                lblAlertNgaySinh.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Gioi tinh
            if (rdbNam.Checked != rdbNu.Checked)
            {
                lblAlertGioiTinh.Visible = false;
            }
            else
            {
                lblAlertGioiTinh.Visible = true;
                isValidately = false;
            }
            #endregion

            #region Check Noi cu ngu
            if (ddlTinhThanh.SelectedIndex <= 0)
            {
                lblAlertTinhThanh.Visible = true;
                isValidately = false;
            }
            else
            {
                lblAlertTinhThanh.Visible = false;
            }
            #endregion
            
            return isValidately;
        }

        #endregion

        protected void rdbNuocKhac_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNuocKhac.Checked)
            {                
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstQuocGia);
            }
            else
            {                
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
            }
        }

        protected void rdbVietNam_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbVietNam.Checked)
            {                
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstTinhThanh);
            }
            else
            {                
                Utility.LoadDropDownList(ref ddlTinhThanh, Utility.LstQuocGia);
            }
        }
    }
}
